home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1997: The Complete Utilities Toolkit / macworld-complete-utilities-1997.iso / Programming / Little Smalltalk v3.1.5 / Smalltalk Source / browser.st < prev    next >
Encoding:
Text File  |  1995-07-03  |  8.5 KB  |  338 lines  |  [TEXT/KAHL]

  1. * ***
  2. * Methods for a class browser
  3. *
  4. * Julian Barkway (c) September 1994 All rights reserved. 
  5. *
  6. * v3.1.0 Initial release
  7. * v3.1.1 -
  8. * v3.1.2 - Changed to allow for different actions for double and single clicks.
  9. *         - Increased use of cascades.
  10. *         - Class>>addSubClass:instanceVariableNames: patched to let through embedded
  11. *           digits.
  12. * v3.1.3 - Dictionary pane no longer gives error if clicked on when empty.
  13. * v3.1.5 - Browser completely re-designed to reflect improvements in the basic protocol
  14. *          for Pane and SelectListPane. Protocol for DictionaryPane absorbed into SelectListPane.
  15. *
  16. * ***
  17.  
  18. Class    BrowserWindow Window browser
  19.  
  20. Class    ClassPane SelectListPane
  21. Class    MethodPane SelectListPane
  22. Class    EditorPane TextPane theClass method compile
  23.  
  24. Class    Browser Object cp mp ep
  25.  
  26. *
  27. * addSubClass:... patched so that it doesn't remove numbers embedded in instance 
  28. * variable names.  The downside is that it will let through some illegal names, but 
  29. * since the compiler will trap these when they are used it probably isn't too bad a
  30. * side-effect.
  31. *
  32. Methods Class 'creation'
  33.     addSubClass: aSymbol instanceVariableNames: aString    | newClass |
  34.         newClass <- Class new; name: aSymbol; superClass: self;
  35.                 variables: 
  36.                   (aString words: [:x | x isAlphaNumeric ]).
  37.         aSymbol assign: newClass.
  38.         classes at: aSymbol put: newClass
  39. ]
  40.  
  41. Methods Class 'display'
  42.     editClass
  43.     " Return an addSubClass:instanceVariableNames: message, filled in with
  44.       data from the Class, ready for evaluation "
  45.     | txt |
  46.         txt <- (self superClass) printString , ' addSubClass: #' , 
  47.                 self printString, newLine , '   instanceVariableNames: '''.
  48.         (variables notNil) ifTrue: [
  49.             variables do: [ :var | txt <- (txt , (var asString) , ' ') ]
  50.         ].
  51.         txt <- txt , ''''.
  52.         ^ txt.
  53. ]
  54.  
  55. *
  56. * We sub-class Window to allow the 'close' message to be neatly trapped without
  57. * needing to make Browser a sub-class of Window (which it clearly isn't)
  58. Methods BrowserWindow 'all'
  59.     openAt: aPosition withSize: aSize
  60.         (title = '') ifTrue: [       " Only false when restoring a saveImage'd window "
  61.             self title: 'Browser ' , (nextBrowserNum asString).
  62.             nextBrowserNum <- nextBrowserNum + 1
  63.         ].
  64.         super openAt: aPosition withSize: aSize
  65. |
  66.     close    | reply |
  67.         (self wantsSave) ifTrue: [
  68.             reply <- smalltalk inquire: ('Save contents of window ', self title, '?').
  69.             (reply isNil) ifTrue: [                "Note: can return nil for Cancel"
  70.                 ^ nil
  71.             ]. 
  72.             reply  ifTrue: [
  73.                 ((self mainPane) saveContents: 1) ifFalse: [
  74.                     ^ nil            "User cancelled save operation"
  75.                 ]
  76.             ]
  77.         ].
  78.         super close.
  79.         browser close
  80. |
  81.     browser: aBrowser
  82.         browser <- aBrowser.
  83. ]
  84.  
  85. *
  86. * A selectable list of all classes in the system.
  87. *
  88. Methods ClassPane 'all'
  89.     openOn: aClassDictionary in: aWindow withSizeFrom: topLeft to: bottomRight
  90.         super openOn: aClassDictionary in: aWindow withSizeFrom: topLeft to: bottomRight.
  91.         self font: 'geneva'; fontSize: 9; typeFace: 2;
  92.              button1Action: #prepareMethodPane:;
  93.              button1DoubleClick: #selectClass:;
  94.              owner: self;
  95.              setText
  96. |
  97.     createPopUpMenu
  98.         pMenu <- PopUpMenu new; title: ''; owner: self; create.
  99.         pMenu addItem: 'Browse Class'     action: #browseClass;
  100.               addItem: 'Inspect Class'    action: #inspectClass;
  101.               addItem: 'Add New Class'    action: #addClass;
  102.               addItem: 'Remove Class'    action: #removeClass;
  103.               disableItem: 1; disableItem: 4.
  104. |
  105.     selectClass: aPoint
  106.         self withSelectedItemSend: #showMethodsForClass: to: parent.
  107. |
  108.     browseClass    
  109.         " browse the given class - modified for v3.1.5"
  110.         self withSelectedItemSend: #editClass: to: parent.
  111. |
  112.     inspectClass
  113.         "Added for v3.1.5"
  114.         self sendToSelectedItem: #inspect
  115. |
  116.     addClass    
  117.         " add a new class "
  118.         parent editNewClass
  119. |
  120.     removeClass
  121.         " Will remove class from symbols dictionary
  122.           when we can figure out how... It's not just 
  123.           a simple matter of invoking removeKey: "
  124.         ^ nil
  125. |
  126.     prepareMethodPane: aPoint
  127.         pMenu enableItem: 1; enableItem: 2; enableItem: 3.
  128.         "pMenu enableItem: 4." "Class removal disabled for now..."
  129.         parent prepareMethodPane
  130. ]
  131.  
  132. *
  133. * A selectable list of all methods pertaining to a selected class
  134. *
  135. Methods MethodPane 'all'
  136.     openOn: aMethodDictionary in: aWindow withSizeFrom: topLeft to: bottomRight
  137.         super openOn: aMethodDictionary in: aWindow withSizeFrom: topLeft to: bottomRight.
  138.         self font: 'geneva'; fontSize: 9; typeFace: 2;
  139.              button1Action: #prepareEditor:;
  140.              button1DoubleClick: #selectMethod:;
  141.              owner: self.
  142. |
  143.     createPopUpMenu
  144.         pMenu <- PopUpMenu new; title: ''; owner: self; create.
  145.         pMenu addItem: 'Add New Method'    action: #addMethod;
  146.               addItem: 'Remove Method'    action: #removeMethod;
  147.               disableItem: 1; disableItem: 2.
  148. |
  149.     prepareEditor: aPoint
  150.         parent cancelEditor.
  151.         pMenu enableItem: 2
  152. |
  153.     selectMethod: aPoint 
  154.         self withSelectedItemSend: #editMethod: to: parent.
  155. |
  156.     addMethod
  157.         method <- Method new.
  158.         parent addNewMethod.
  159. |
  160.     removeMethod
  161.         self withSelectedItemSend: #removeMethod: to: self
  162. |
  163.     removeMethod: m
  164.     | t |
  165.         " Remove given method from currently selected class "
  166.         (m notNil) ifTrue: [
  167.             t <- (smalltalk inquire: 'Remove method ''', (m name), '''?').
  168.             (t isNil) ifTrue: [
  169.                 ^ nil
  170.             ].
  171.             t ifTrue: [
  172.                 collection removeKey: (m name).
  173.                 self setText.
  174.                 pMenu disableItem: 2
  175.             ]
  176.         ]
  177. |
  178.     showMethodsForClass: c
  179.         self collection: (c methods); setText.
  180.         pMenu enableItem: 1; disableItem: 2.
  181.         parent setClass: c
  182. |
  183.     clearPane
  184.         self clearAllText.
  185.         pMenu disableItem: 1; disableItem: 2.
  186.         parent cancelEditor
  187. ]
  188.  
  189. *
  190. * A pane which allows methods and classes to be viewed, edited and compiled into the system.
  191. *
  192. Methods EditorPane 'all'
  193.     openIn: aWindow withSizeFrom: topLeft to: bottomRight
  194.         self boundsFrom: topLeft to: bottomRight;
  195.              owner: self;
  196.              attachTo: aWindow withSizing: (1 @ 1);
  197.              font: 'monaco'; fontSize: 9.
  198. |
  199.     parent: anObject
  200.         parent <- anObject
  201. |
  202.     createPopUpMenu
  203.         pMenu <- PopUpMenu new; 
  204.             owner: self;
  205.             title: '';
  206.             create.
  207.         pMenu addItem: 'Accept' action: #accept;
  208.               addItem: 'Cancel' action: #cancel;
  209.               disableItem: 1; disableItem: 2.
  210.         compile <- true
  211. |
  212.     setClass: c
  213.         theClass <- c
  214. |
  215.     editClass: c
  216.         self clearAllText.
  217.         self print: (c editClass).
  218.         theClass <- c.
  219.         compile  <- false.
  220.         pMenu disableItem: 1; enableItem: 2.
  221. |
  222.     editNewClass
  223.         self clearAllText;
  224.            print: 'superClass addSubClass: #nameOfClass ', newLine,
  225.                    '  instanceVariableNames: ''var1 var2'' '.
  226.         compile <- false.
  227.         pMenu enableItem: 1; enableItem: 2.
  228. |
  229.     editMethod: m
  230.         self print: m text.
  231.         compile <- true.
  232.         method <- m.
  233.         pMenu enableItem: 1; enableItem: 2
  234. |
  235.     editNewMethod
  236.         self clearAllText;
  237.              print: '  "A comment stating the method''s function"' , newLine;
  238.              print: '  messageSelector: argumentNames " -- argument names optional"', newLine;
  239.              print: '    | temporaries | "-- temporaries optional"', newLine;
  240.              print: '    body of method', newLine.
  241.         compile <- true.
  242.         method  <- Method new.
  243.         pMenu enableItem: 1; enableItem: 2.
  244. |
  245.     accept
  246.         compile ifTrue:  [ self compile   ]
  247.                 ifFalse: [ self doCommand ]
  248. |
  249.     cancel
  250.         self clearAllText.
  251.         pMenu disableItem: 1; disableItem: 2.
  252.  
  253. |
  254.     compile
  255.         method text: (self text).
  256.         (method compileWithClass: theClass)
  257.             ifTrue: [ 
  258.                 theClass methods at: method name put: method.
  259.                 parent showMethods
  260.             ].
  261. |
  262.     doCommand
  263.         " accept tw command "
  264.         [ self text execute. parent showClasses ] fork.
  265. |
  266.     close
  267.         pMenu dispose
  268. ]
  269.  
  270. *
  271. * The actual browser object is just a co-ordinator for the three panes which do
  272. * the real work.
  273. *
  274. Methods Browser 'all'
  275.     open 
  276.     | bwin maxW maxH | 
  277.         maxW <- (smalltalk getMaxScreenArea) right.
  278.         maxW <- 450 min: (maxW - 70).
  279.         maxH <- (smalltalk getMaxScreenArea) bottom.
  280.         maxH <- 500 min: (maxH - 70).
  281.         bwin <- BrowserWindow new; 
  282.             browser: self;
  283.             openAt: (20@60) withSize: (maxW@maxH).
  284.         self makePanes: bwin.
  285. |
  286.     makePanes: bwin
  287.     | ww wh ph pw |
  288.         ww <- (bwin size) x.
  289.         wh <- (bwin size) y.
  290.         pw <- (ww / 2) truncated.
  291.         ph <- (wh / 5) truncated.
  292.         cp <- ClassPane new;
  293.                 openOn: classes in: bwin withSizeFrom: (-1 @ -1) to: (pw @ ph); parent: self.
  294.         mp <- MethodPane new;
  295.                 openOn: nil in: bwin withSizeFrom: ((pw - 1) @ -1) to: ((ww + 1) @ ph);
  296.                 parent: self.
  297.         ep <- EditorPane new;
  298.                 openIn: bwin withSizeFrom: (-1 @ (ph - 1)) to: ((ww + 1) @ (wh + 1));
  299.                 parent: self.
  300. |
  301.     setClass: c
  302.         ep setClass: c
  303. |
  304.     showClasses
  305.         cp setText
  306. |
  307.     showMethodsForClass: c
  308.         mp showMethodsForClass: c
  309. |
  310.     editClass: c
  311.         ep editClass: c
  312. |
  313.     editNewClass
  314.         ep editNewClass
  315. |
  316.     showMethods 
  317.         mp setText
  318. |
  319.     prepareMethodPane
  320.         mp clearPane
  321. |
  322.      editMethod: m
  323.          ep editMethod: m
  324. |
  325.     addNewMethod
  326.         ep editNewMethod
  327. |
  328.     cancelEditor
  329.         ep cancel
  330. |
  331.     close
  332.         " close our window and remove the pop-up menus"
  333.         cp close.
  334.         mp close.
  335.         ep close.
  336. ]
  337.